package p.ithorns.tools.validation;

/**
 * StringUtils
 * TODO
 *
 * @author HtL
 * @date 2024/8/14 15:16
 * @since 1.0.0
 */
 final class StringUtil {

    /**
     * 判空(包括空白字符串)
     *
     * @param cs -源
     * @return String
     */
    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for (int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    /**
     * 去除首尾空格(中文/全角)
     */
    public static String trimLeadTailWhitespace(String str) {
        if (!hasLength(str)) {
            return str;
        }

        int beginIdx = 0;
        while (beginIdx < str.length() && Character.isWhitespace(str.charAt(beginIdx))) {
            beginIdx++;
        }
        str = str.substring(beginIdx);

        int endIdx = str.length() - 1;
        while (endIdx >= 0 && Character.isWhitespace(str.charAt(endIdx))) {
            endIdx--;
        }
        return str.substring(0, endIdx + 1);
    }

    /**
     * 判断是否有长度
     *
     * @param src
     * @return boolean
     */
    public static boolean hasLength(String src) {
        return src != null && !src.isEmpty();
    }

}